﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using HoneyBadgersOfficePlayer.Models;
using System.Data.SqlClient;

namespace HoneyBadgersOfficePlayer.Views
{
    public partial class Upload : System.Web.UI.Page
    {
        protected void UploadButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (FileUploadControl.HasFile)
                {
                    string filename = Path.GetFileName(FileUploadControl.FileName);
                    string path = string.Empty;
                    string currentDirectory = Environment.CurrentDirectory;
                    string rootDirectory = Directory.GetDirectoryRoot(currentDirectory) + "Music";

                    foreach (string file in Directory.GetFiles(rootDirectory))
                    {
                        if (file == (rootDirectory + "\\" + filename))
                        {
                            path = file;
                            break;
                        }
                    }

                    if (path != string.Empty)
                    {
                        SCPUtilities.Upload(filename, path);
                        StatusLabel.Text = "Upload status: File uploaded!";
                     
                        string title = GetTitle(path);
                        string artistName = GetArtist(path);
                        string album = GetAlbum(path);
                        int songLength = GetSongLength(path);
                        DateTime releaseDate = GetReleaseDate(path);
                        int explicitLyrics = GetExplicit(path);

                        List<SqlParameter> parameterList = new List<SqlParameter>();
                        parameterList.Add(new SqlParameter("@name", title));
                        parameterList.Add(new SqlParameter("@artistName", artistName));
                        parameterList.Add(new SqlParameter("@album", album)); 
                        parameterList.Add(new SqlParameter("@songLength", songLength));
                        parameterList.Add(new SqlParameter("@releaseDate", releaseDate));
                        parameterList.Add(new SqlParameter("@userID", Globals.UserID));
                        parameterList.Add(new SqlParameter("@fileLocation", "http://icarus.cs.weber.edu/~honeybadgers/Music Files/" + Globals.UserID + "/" + filename));
                        parameterList.Add(new SqlParameter("@explicit", explicitLyrics));
                        parameterList.Add(new SqlParameter("@artwork", null));

                        SQLUtilities.RunStoredProcedure("sp_InsertNewSong", parameterList);
                    }
                    else
                    {
                        StatusLabel.Text = "Upload status: The file could not be uploaded. Music files must be located in C:\\Music in order to be uploaded successfully.";
                    }
                }
                else
                {
                    StatusLabel.Text = "Upload status: Only MP3 files are accepted!";
                }
            }
            catch (Exception ex)
            {
                StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }

        //function that will return the title of the song
        public string GetTitle(string path)
        {
            try
            {
                TagLib.File mp3 = TagLib.File.Create(path);
                string songtitle = mp3.Tag.Title;

                if (!string.IsNullOrEmpty(songtitle))
                {
                    return songtitle;
                }
                else
                {
                    return "Untitled";
                }
            }
            catch(TagLib.CorruptFileException ex)
            {
                return ex.Message;
            }
        }

        public string GetArtist(string path)
        {
            try
            {
                TagLib.File mp3 = TagLib.File.Create(path);
                string songartist = mp3.Tag.FirstPerformer;

                if (!string.IsNullOrEmpty(songartist))
                {
                    return songartist;
                }
                else
                {
                    return "Unknown";
                }
            }
            catch (TagLib.CorruptFileException ex)
            {
                return ex.Message;
            }
        }

        public string GetAlbum(string path)
        {
            try
            {
                TagLib.File mp3 = TagLib.File.Create(path);
                string album = mp3.Tag.Album;

                if (!string.IsNullOrEmpty(album))
                {
                    return album;
                }
                else
                {
                    return "Unknown";
                }
            }
            catch (TagLib.CorruptFileException ex)
            {
                return ex.Message;
            }
        }

        public int GetSongLength(string path)
        {
            try
            {
                TagLib.File mp3 = TagLib.File.Create(path);
                int songLength = Convert.ToInt16(mp3.Properties.Duration.TotalSeconds);

                if (songLength != 0)
                {
                    return songLength;
                }
                else
                {
                    return 0;
                }
            }
            catch (TagLib.CorruptFileException ex)
            {
                return 0;
            }
        }

        public DateTime GetReleaseDate(string path)
        {
            try
            {
                TagLib.File mp3 = TagLib.File.Create(path);
                DateTime releaseDate = new DateTime();
                DateTime.TryParse("01-01-" + Convert.ToString(mp3.Tag.Year), out releaseDate);

                if (releaseDate != null)
                {
                    return releaseDate;
                }
                else
                {
                    return DateTime.Now;
                }
            }
            catch (TagLib.CorruptFileException ex)
            {
                return DateTime.Now;
            }
        }

        public int GetExplicit(string path)
        {
            return 0;
        }
    }
}